package GateWars;

import List.PlayerNode;
import List.SortedList;

public class GateWarsManager extends SendMessage implements Runnable {
	private int min = 50, max = 60;
	
	private boolean loggedIn = false;	
	protected int page = 0;
	protected boolean updated = false;
	private boolean running = false;
	/**
	 * Player attributes
	 */
	protected short turns;
	protected long naq;
	protected long bankNaq;
	protected byte nextTurn;
	protected String name;
	protected boolean perg;
	protected int unitPord;
	protected byte covertCap;
	protected long stike;
	protected long defence;
	protected long covert;
	protected long antiCov;
	protected long MSAction;
	protected int AttackDrone;
	protected int AttackClone;
	protected int AttackMercenaries;
	protected int DefenseDrone;
	protected int DefenseClone;
	protected int DefenseMercenaries;
	protected int GeneralDrone;
	protected int MiningDrone;
	protected int lifers;
	protected int UntrainedMercenaries;
	protected int UndercoverAgents;
	protected int AntiIntelligenceDrone;
	protected int TotalFightingForce;
	protected SortedList alp = new SortedList();
	
	public GateWarsManager(String cookie){
		Cookie = cookie;
		loggedIn = true;
	}
	
	public boolean getLoggedIn(){
		return loggedIn;
	}
	
	protected synchronized void fParsingBFPage(int page, SortedList s){
		for(PlayerNode n : s){
            if(n.getArmySize() > 100000000 && n.getId() != 0)
            {
                //System.out.println(n.getId() + " : " + n.getAlliance() + " : " + n.getRelation());
                alp.addPlayer(n.clone());
                //System.out.println("added : " + n.getName() + " : " + n.getArmySize());
            }				
        }
        if (alp.count > 10) {
            for(PlayerNode n : alp){
                //<a href=stats.php?id=1962905>digi </a>
//                System.out.println(n.getName() + " : " + n.getId() + " : " + n.getArmySize() + " : " + n.getAlliance());
                if (n.getAlliance() == null) {
                    System.out.println("<a href=stats.php?id=" + n.getId() + ">" + n.getName() + " </a> : " + n.getArmySize());
                } else {
                    System.out.println("<a href=stats.php?id=" + n.getId() + ">" + n.getName() + " [ " + n.getAlliance() + " ] </a> : " + n.getArmySize());
                }
            }
            alp = new SortedList();
        }
        /*this.page = page;
        if(page > min && page <= max && page <= 2677){
            //System.out.println(page);
            for(PlayerNode n : s){
                if(n.getArmySize() > 100000000 && n.getId() != 0)
                {
                    //System.out.println(n.getId() + " : " + n.getAlliance() + " : " + n.getRelation());
                    alp.addPlayer(n.clone());
                    //System.out.println("added : " + n.getName() + " : " + n.getArmySize());
                }				
            }
//                BattlefieldBattlefieldMessage(page + 1);
        }
        else if (page > 50 && page <= 2677 && (running || page == 51))
        {
            running = true;
            for(PlayerNode n : s){
                if(n.getArmySize() > 100000000 && n.getId() != 0)
                {
                    alp.addPlayer(n.clone());
                }				
            }
            min += 10;
            max += 10;
            for(PlayerNode n : alp){
                System.out.println(n.getName() + " : " + n.getId() + " : " + n.getArmySize() + " : " + n.getAlliance());
            }
            alp = new SortedList();
//                BattlefieldBattlefieldMessage(page + 1);
        }*/
		updated = true;
	}
	
	/**
	 * Saves account information like army size and available turns...
	 * The list of "Targets" will be persistent between different accounts.
	 * An avoid list will however be loaded but not saved for each account.
	 */
	private void save(){
		loggedIn = false;
	}
	
	private void load(){
		loggedIn = true;
	}
	
	protected void logOut(){
		save();
		running = false;
	}

	@Override
	public void run() {
		try {
			load();
			running = true;
			updated = false;
            page = 51;
			BaseBattlefieldMessage(page);
			while(!updated)
				Thread.sleep(2500);
			updated = false;
            
            while (page < 2677) {
                BattlefieldBattlefieldMessage(page++);
				Thread.sleep(500);
            }
            
            /*
			JTBattlefieldPageMessage(page, 51);
			while(!updated)
				Thread.sleep(2500);
			System.out.println(page);
			while(running && page < 2677)
			{
				while(!updated)
						Thread.sleep(500);
				updated = false;
				BattlefieldBattlefieldMessage(page + 1);
			}
            */
			/*for(PlayerNode n : GateWarsFactory.getPlayerList())
			{
				if(n.getArmySize() > 2000000)
					System.out.println(n.getName() + " " + n.getArmySize());
			}*/
		} catch (InterruptedException e) {
//			e.printStackTrace();
		}
	}
}
